﻿@page "/mainplan/update/{Id}"
@inject IMainPlanService MainPlanService
@inject NavigationManager NavigationManager
@inject IJSRuntime JsRuntime

<PageTitle>更新主计划</PageTitle>

<div class="row">
    <div class="col">
        <p class="fs-5">更新主计划信息</p>
    </div>
    @* 不允许删除计划 *@
    @* <CompDeleteIcon Roles="admin,pmc" Click="Delete" /> *@
</div>
<EditForm Model="_mainPlanDto" OnSubmit="Save">
    <div class="row">
        <div class="col input-group mb-2">
            <span class="input-group-text">创建时间</span>
            <input type="date" class="form-control" @bind-value="@_mainPlanDto.CreateTime" />
        </div>
        <div class="col input-group mb-2">
            <span class="input-group-text">项目月份</span>
            <input type="month" class="form-control" @bind-value="@_mainPlanDto.MonthOfInvoice" />
        </div>
        <div class="col"></div>
        <div class="col"></div>
    </div>
    <div class="row">
        <div class="col input-group mb-2">
            <span class="input-group-text">完工时间</span>
            <input type="date" class="form-control" @bind-value="@_mainPlanDto.FinishTime" />
        </div>
        <div class="col input-group mb-2">
            <span class="input-group-text">计划发图</span>
            <input type="date" class="form-control" @bind-value="@_mainPlanDto.DrwReleaseTarget" />
        </div>
        <div class="col"></div>
        <div class="col"></div>
    </div>
    <div class="row">
        <div class="col input-group mb-2">
            <span class="input-group-text">编号</span>
            <input class="form-control" placeholder="编号" @bind-value="@_mainPlanDto.Number" />
        </div>
        <div class="col input-group mb-2">
            <span class="input-group-text">分批</span>
            <input class="form-control" type="number" @bind-value="@_mainPlanDto.Batch" />
        </div>
        <div class="col"></div>
        <div class="col"></div>
    </div>
    <div class="input-group mb-2">
        <span class="input-group-text">项目名称</span>
        <input class="form-control" placeholder="项目名称" @bind-value="@_mainPlanDto.Name" />
    </div>
    <div class="input-group mb-2">
        <span class="input-group-text">烟罩数量</span>
        <input class="form-control" type="number" @bind-value="@_mainPlanDto.Quantity" />
    </div>
    <div class="input-group mb-2">
        <span class="input-group-text">烟罩型号</span>
        <input class="form-control" placeholder="型号" @bind-value="@_mainPlanDto.ModelSummary" />
    </div>
    <div class="input-group mb-2">
        <span class="input-group-text">项目类型</span>
        <select class="form-select" @bind="@_mainPlanDto.MainPlanType">
            <CompEnumList Type="typeof(MainPlanType_e)"></CompEnumList>
        </select>
    </div>
    <div class="input-group mb-2">
        <span class="input-group-text">项目备注</span>
        <textarea class="form-control" placeholder="备注，可输入多行" style="height: 120px" @bind="@_mainPlanDto.Remarks"></textarea>
    </div>
    
    <CompSaveCancelButtons CancelUrl="mainplans" />
</EditForm>

@code {
    [Parameter]
    public string Id { get; set; }
    private MainPlanDto? _mainPlanDto = new();
    protected override async Task OnInitializedAsync()
    {
        var response = await MainPlanService.GetFirstOrDefaultAsync(Guid.Parse(Id));
        if (!response.Status) NavigationManager.NavigateTo("mainplans");
        _mainPlanDto = response.Result;
    }
    private async Task Delete()
    {
        var confirmResult = await JsRuntime.InvokeAsync<bool>("confirm", $"确定要删除生产计划【{_mainPlanDto.Name}】吗？");
        if (confirmResult)
        {
            var result = await MainPlanService.DeleteAsync(Guid.Parse(Id));
            if (result.IsSuccessStatusCode) NavigationManager.NavigateTo("mainplans");
        }
    }

    private async Task Save()
    {
        if (string.IsNullOrWhiteSpace(_mainPlanDto.Number)||string.IsNullOrWhiteSpace(_mainPlanDto.Name))
        {
            await JsRuntime.InvokeVoidAsync("alert", "填写的信息不完整");
            return;
        }
        //为了避免FSO号前后有空格，导致后续创建文件夹时出现错误，此处消除空格
        _mainPlanDto.Number = _mainPlanDto.Number.Trim().ToUpper();
        var same = await MainPlanService.GetFirstOrDefaultByNumberAsync(_mainPlanDto.Number);
        if (same.Status && same.Result.Id!=_mainPlanDto.Id)
        {
            await JsRuntime.InvokeVoidAsync("alert", "项目编号重复，请重新修改项目编号！");
            return;
        }
        var result = await MainPlanService.UpdateAsync(Guid.Parse(Id), _mainPlanDto);
        if (result.IsSuccessStatusCode) NavigationManager.NavigateTo("mainplans");
    }
}
